package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 

    public DvdUI()
    {
        dvdCtr = dvdCtr.getInstance();
    }

    public void start()
    {

        dvdMenu();
    }

    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeDVDMenu();
            switch(choice){
                case 1:
                //Show DVD information
                findDVD();
                break;
                case 2:
                //Create DVD
                createDVD();
                break;
                case 3:
                //Update DVD
                updateDVD();
                break;
                case 4:
                //Delete DVD
                deleteDVD();
                break;
                case 5:
                //read Copy
                findCopy();
                break;
                case 6:
                //create Copy                
                createCopy();
                break; 
                case 7:
                //update Copy
                updateCopy();
                break; 
                case 8:
                //delete Copy
                deleteCopy();
                break; 
                case 9:
                //Show a list of all DVDs
                listAllDvds();
                break; 
                case 10:
                //Show all copies to all DVDs
                listAllDvdsAndCopies();
                break;                 
                default:
                exit = true;
                break;
            }//end else
        }//end else
    }//end else

    private int writeDVDMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** DVD Menu ***");
        System.out.println(" (1) Find a DVD and show its copies");
        System.out.println(" (2) Create DVD");
        System.out.println(" (3) Update DVD");
        System.out.println(" (4) Delete DVD"); 
        System.out.println("--------------------------------------");
        System.out.println(" (5) Find a Copy");
        System.out.println(" (6) Create Copy");
        System.out.println(" (7) Update Copy");
        System.out.println(" (8) Delete Copy");  
        System.out.println("--------------------------------------");
        System.out.println(" (9) Show all DVDs");
        System.out.println(" (10) Show all copies to all DVDs");         
        System.out.println(" (11) Return");
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    private int inputDVDNo()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Type DVD number: ");
            try{
                id = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;

    }

    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }

    private String inputAcDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type copy' Acquiring date: " );
        String title = keyboard.nextLine();
        return title;
    }

    public int inputAcPrice()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write Acquiring Price:  ");
            try{
                id = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
    }

    private String inputReleaseDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type release date: " );
        String title = keyboard.nextLine();
        return title;
    }

    private String inputPublisher()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the publisher: " );
        String title = keyboard.nextLine();
        return title;
    }

    private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy number: ");
        int no = keyboard.nextInt();
        return no;
    }

    //there is need for more methods to read information about DVDs
    //and methods to display the information

    private void findDVD()
    {     
        Scanner keyboard = new Scanner(System.in);
        int no = inputDVDNo();
        if(dvdCtr.findDVD(no) != null)
        {
            dvdCtr.findDVD(no).printDetailDVD();

            System.out.println();
            System.out.println("Enter to continue...");

        }
        else
        {
            System.out.println();
            System.out.println("No DVDs have been created !");
            System.out.println();
            System.out.println("Enter to continue...");

        }
        keyboard.nextLine();
    }

    private void findDVDandCopies()
    {     
        Scanner keyboard = new Scanner(System.in);
        int no = inputDVDNo();
        if(dvdCtr.findDVD(no) != null)
        {
            dvdCtr.findDVD(no).printAllCopy();       
            System.out.println();
            System.out.println("Enter to continue...");

        }
        else
        {
            System.out.println();
            System.out.println("No DVDs have been created !");
            System.out.println();
            System.out.println("Enter to continue...");

        }
        keyboard.nextLine();
    }

    private void createDVD()
    {
        Scanner keyboard = new Scanner(System.in);

        dvdCtr.createDVD(inputTitle(), inputPublisher(), inputReleaseDate());
        int id = dvdCtr.getID(); 
        if(dvdCtr.findDVD(id) != null)
        {
            System.out.println();
            System.out.println("New DVD "+ id+" is created - se info above");
            dvdCtr.findDVD(id).printDetailDVD();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("Something went wrong - contact an administrator or try again! ");
            System.out.println();
            System.out.println("Enter to continue...");
        }
        keyboard.nextLine();

    }
    
    private void deleteDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        int id = inputDVDNo();
        if(dvdCtr.findDVD(id) != null)
        {        
            System.out.println();
            System.out.println("The DVD with the id: "+id+" is now deleted");
            System.out.println();
            dvdCtr.findDVD(id).printDetailDVD();
            System.out.println();
            System.out.println("Enter to continue...");
            keyboard.nextLine();
            dvdCtr.deleteDVD(id);
        }
        else
        {
            System.out.println();
            System.out.println("No DVDs is deleted - id "+id+" dosen't exist !");
            System.out.println();
            System.out.println("Enter to continue...");
            keyboard.nextLine();
        }
        
    }
    
    private void updateDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        int idDVD = inputDVDNo();
        String title = inputTitle();
        String publisher = inputPublisher();
        String releasedate = inputReleaseDate();
        
        if(dvdCtr.findDVD(idDVD) != null)
        {        
            dvdCtr.updateDVD(idDVD, title, publisher, releasedate);
            System.out.println();
            System.out.println("The DVD with the id: "+idDVD+" is now updated");
            System.out.println();
            dvdCtr.findDVD(idDVD).printDetailDVD();
            System.out.println();
            System.out.println("Enter to continue...");           
        }
        else
        {
            System.out.println();
            System.out.println("No DVDs is updated - id "+idDVD+" dosen't exist !");
            System.out.println();
            System.out.println("Enter to continue...");
            
        }
        keyboard.nextLine();
        
    }

    private void createCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        int id = inputDVDNo();
        if(dvdCtr.findDVD(id) != null)
        {
            dvdCtr.findDVD(id).createCopy(inputAcDate(), inputAcPrice());
            int id1 = dvdCtr.getSerieNoCopy(id);

            System.out.println();
            System.out.println("New Copy "+ id1+" is created - se info above");
            
            dvdCtr.findDVD(id).findCopy(id1).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");

        }
        else
        {
            System.out.println();
            System.out.println("No DVDs have been created !");
            System.out.println();
            System.out.println("Enter to continue...");

        }
        keyboard.nextLine();

    }
    
    private void findCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        int idDVD = inputDVDNo();
        int idCOPY = inputCopyNo();
        
        if(dvdCtr.findDVD(idDVD) != null && dvdCtr.findDVD(idDVD).findCopy(idCOPY) !=null)
        {                    
            System.out.println("The Copy with id "+idCOPY+" on DVD with id "+idDVD);            
            dvdCtr.findDVD(idDVD).findCopy(idCOPY).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("No DVDs or copies have been created !");
            System.out.println();
            System.out.println("Enter to continue...");
            
        }  
        keyboard.nextLine();
    } 
    
    private void updateCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        int idDVD = inputDVDNo();
        int idCOPY = inputCopyNo();
        String acDate = inputAcDate();
        int acPrice = inputAcPrice();
        if(dvdCtr.findDVD(idDVD) != null && dvdCtr.findDVD(idDVD).findCopy(idCOPY) !=null)
        {
            dvdCtr.updateCopy(idDVD, idCOPY, acDate, acPrice);
            
            System.out.println("The Copy with id "+idCOPY+" is update");
            dvdCtr.findDVD(idDVD).findCopy(idCOPY).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("No DVDs or copies have been created !");
            System.out.println();
            System.out.println("Enter to continue...");

        }
        keyboard.nextLine();
    
    }
    
    private void deleteCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        int idDVD = inputDVDNo();
        int idCOPY = inputCopyNo();        
        if(dvdCtr.findDVD(idDVD) != null && dvdCtr.findDVD(idDVD).findCopy(idCOPY) !=null)
        {                    
            System.out.println("The Copy with id "+idCOPY+" is deleted");  
            dvdCtr.findDVD(idDVD).findCopy(idCOPY).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");
            keyboard.nextLine();
            dvdCtr.deleteCopy(idDVD, idCOPY);
        }
        else
        {
            System.out.println();
            System.out.println("No DVDs with id"+idDVD+" or copies with id"+idCOPY+" have been created !");
            System.out.println();
            System.out.println("Enter to continue...");
            keyboard.nextLine();
        }  
    }    

    private void listAllDvds()
    {
        Scanner keyboard = new Scanner(System.in);

        if(dvdCtr.getSize() != 0)
        {
            dvdCtr.listAllDvd();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("There is no DVDs ");
            System.out.println();
            System.out.println("Enter to continue...");
        }
        keyboard.nextLine();
    }

    private void listAllDvdsAndCopies()
    {
        Scanner keyboard = new Scanner(System.in); 

        if(dvdCtr.getID() != 0 && dvdCtr.getSize() != 0)
        {
            dvdCtr.listAllDVDAndCopies();
            System.out.println();
            System.out.println("Enter to continue...");
        }   
       
        else
        {
            System.out.println();
            System.out.println("There is no DVDs ");
            System.out.println();
            System.out.println("Enter to continue...");
        }
        keyboard.nextLine();
    }
}